home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / pixmap.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  9KB  |  362 lines

  1. /*
  2.  * (c) Copyright 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. /*
  39. ** $Revision: 1.2 $
  40. ** $Date: 1995/02/05 22:13:54 $
  41. */
  42. /*
  43. **  This program is to demonstrate the use of OpenGL with both X windows
  44. **  and pixmaps.  One context is shared between a pixmap and a window.
  45. **  Note that since the default is to render to the pixmap, no image will
  46. **  be drawn in the window when it first comes up.
  47. **
  48. **  The picture consists of three triangles clipped and
  49. **  layered using depth and scissor tests.
  50. **
  51. **  The user controls rendering with three keys:
  52. **    w    Render to the window 
  53. **    p    Render to the pixmap (default)
  54. **    c    Copy the contents of the pixmap into the window
  55. **
  56. **  The program runs in rgb mode by default.  To run in color index mode,
  57. **  use the command line option -c.
  58. */
  59.  
  60. #include <GL/glx.h>
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <X11/keysym.h>
  64.  
  65. static int RGBattributes[] = {
  66.     GLX_RGBA,
  67.     GLX_RED_SIZE, 1,
  68.     GLX_GREEN_SIZE, 1,
  69.     GLX_BLUE_SIZE, 1,
  70.     None,
  71. };
  72.  
  73. static int CIattributes[] = {
  74.     GLX_DEPTH_SIZE, 1,
  75.     None,
  76. };
  77.  
  78. int width = 200, height = 200;
  79.  
  80. static void Reshape(int w, int h)
  81. {
  82.     glViewport(0, 0, (GLint)w, (GLint)h);
  83.  
  84.     width = w;
  85.     height = h;
  86.  
  87.     glMatrixMode(GL_PROJECTION);
  88.     glLoadIdentity();
  89.     glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
  90.     glMatrixMode(GL_MODELVIEW);
  91. }
  92.  
  93.  
  94. static void Init(void)
  95. {
  96.  
  97.     glClearColor(0.0, 0.0, 0.0, 0.0);
  98.     glClearIndex(1.0);
  99.  
  100.     glClearDepth(1);
  101.     glClearStencil(0);
  102.     glEnable(GL_DEPTH_TEST);
  103. }
  104.  
  105.  
  106.  
  107. static void DoDisplay(void)
  108. {
  109.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  110.  
  111.     glColor3ub(200, 0, 0);
  112.     glIndexi(2);
  113.     glBegin(GL_POLYGON);
  114.         glVertex3i(-4, -3, -2);
  115.         glVertex3i( 4, -3, -2);
  116.         glVertex3i( 0,  4, -2);
  117.     glEnd();
  118.     glFlush();
  119.  
  120.     glEnable(GL_SCISSOR_TEST);
  121.     glScissor(width/3, height/3, width/3, height/3);
  122.     glColor3ub(0, 200, 0);
  123.     glIndexi(29);
  124.     glBegin(GL_POLYGON);
  125.         glVertex3i(-4, -3, 0);
  126.         glVertex3i( 4, -3, 0);
  127.         glVertex3i( 0,  4, 0);
  128.     glEnd();
  129.     glDisable(GL_SCISSOR_TEST);
  130.     glFlush();
  131.  
  132.     glColor3ub(0, 0, 200);
  133.     glIndexi(31);
  134.     glBegin(GL_POLYGON);
  135.         glVertex3i(-4, -4, -1);
  136.         glVertex3i( 4, -4, -1);
  137.         glVertex3i( 0,  3, -1);
  138.     glEnd();
  139.     glFlush();
  140.  
  141. }
  142.  
  143.  
  144. static void Usage(void)
  145. {
  146.     printf("Usage: pixmap [-c]\n");
  147.     printf("   -c:  Run in color index mode\n");
  148.     exit(-1);
  149. }
  150.  
  151.  
  152. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  153. {
  154.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  155.     return GL_TRUE;
  156.     }
  157.     return GL_FALSE;
  158. }
  159.  
  160. int main(int argc, char *argv[])
  161. {
  162.     /* X variables */
  163.     XVisualInfo *vi;
  164.     Display *dpy;
  165.     Colormap cmap;
  166.     Window window;
  167.     Window root;
  168.     Pixmap pixmap;
  169.     XSetWindowAttributes swa;
  170.     XEvent event;
  171.     GC gc;
  172.  
  173.     /* GLX variables */
  174.     GLXContext cx;
  175.     GLXPixmap pm;
  176.  
  177.     GLboolean needDisplay;
  178.     int gx, gy, i;
  179.     unsigned int gwidth, gheight, gborder_width, gdepth;
  180.     Status xggStatus;
  181.     int rgb = 1;    /* Use rgb mode by default */
  182.  
  183.     /*
  184.     ** Read command line arguments.
  185.     */
  186.     for (i = 1; i < argc; i++) {
  187.         if (argv[i][0] == '-') {
  188.             switch (argv[i][1]) {
  189.               case 'c':
  190.                 rgb = 0;
  191.                 break;
  192.               default:
  193.                 Usage();
  194.             }
  195.         } else {
  196.             Usage();
  197.         }
  198.     }
  199.  
  200.  
  201.     /*
  202.     ** Open the X connection.
  203.     */
  204.     dpy = XOpenDisplay(0);
  205.     if (!dpy) {
  206.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  207.     return -1;
  208.     }
  209.  
  210.     /*
  211.     ** Select the X visual enabled for OpenGL, using RGB or CI mode.
  212.     */
  213.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), 
  214.         rgb ? RGBattributes : CIattributes);
  215.     if (!vi) {
  216.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  217.         getenv("DISPLAY"));
  218.     return -1;
  219.     }
  220.  
  221.     /*
  222.     ** Create a colormap.
  223.     ** If its RGB, then the visual is TrueColor and the map is already
  224.     ** allocated read-only.  For CI, allocate the entire map.
  225.     */
  226.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  227.                rgb ? AllocNone: AllocAll);
  228.     /* XXX 
  229.     ** could use some colormap setting here.
  230.     */
  231.  
  232.     /* 
  233.     ** Set the window attributes in the  XSetWindowAttributes structure.
  234.     ** Ask for events at exposure, change in window structure, key
  235.     ** presses.
  236.     */
  237.     swa.border_pixel = 0;
  238.     swa.colormap = cmap;
  239.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  240.     | KeyReleaseMask;
  241.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  242.                width, height,
  243.                0, vi->depth, InputOutput, vi->visual,
  244.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  245.     XSetStandardProperties(dpy, window, "pixmap", "pixmap", None, argv, argc, 
  246.     NULL);
  247.     XSetWMColormapWindows(dpy, window, &window, 1);
  248.  
  249.     /*
  250.     ** Make the window visible by mapping it.
  251.     */
  252.     XMapWindow(dpy, window);
  253.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  254.  
  255.     /*
  256.     ** Create an X graphics context.
  257.     */
  258.     gc = XCreateGC(dpy, window, 0, NULL);
  259.  
  260.     /*
  261.     **  Create a pixmap, using the same depth as the window visual.
  262.     **  Next, create a a GLX rendering area using that pixmap.
  263.     */
  264.     pixmap = XCreatePixmap(dpy, RootWindow(dpy, vi->screen), 
  265.     width, height, vi->depth);
  266.     pm = glXCreateGLXPixmap(dpy, vi, pixmap);
  267.     xggStatus = XGetGeometry(dpy, pixmap, &root, &gx, &gy, &gwidth, &gheight,
  268.     &gborder_width, &gdepth);
  269.  
  270.     /*
  271.     ** Create an OpenGL context, and make it current to the pixmap.
  272.     */
  273.     cx = glXCreateContext(dpy, vi, 0, GL_FALSE);
  274.     if (!glXMakeCurrent(dpy, pm, cx)) {
  275.     fprintf(stderr, "Can't make pixmap current to context\n");
  276.     return -1;
  277.     }
  278.  
  279.  
  280.     printf("rendering to pixmap\n");
  281.     Init();
  282.  
  283.     needDisplay = GL_TRUE;
  284.  
  285.     /*
  286.     ** Main event loop
  287.     */
  288.     for (;;) {
  289.     do {
  290.         XNextEvent(dpy, &event);
  291.         switch (event.type) {
  292.           case Expose:
  293.         needDisplay = GL_TRUE;
  294.         break;
  295.           case ConfigureNotify:
  296.         width = event.xconfigure.width;
  297.         height = event.xconfigure.height;
  298.         Reshape(width, height);
  299.         needDisplay = GL_TRUE;
  300.         break;
  301.           case KeyPress:
  302.         {
  303.             char buf[100];
  304.             int rv;
  305.             KeySym ks;
  306.  
  307.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  308.             switch (ks) {
  309.  
  310.               case XK_c:
  311.             /*
  312.             ** Copy pixmap into window.
  313.             */
  314.             xggStatus = XGetGeometry(dpy, window, &root, &gx, &gy, 
  315.                 &gwidth, &gheight, &gborder_width, &gdepth);
  316.             XCopyArea(dpy, pixmap, window, gc, 0, 0, gwidth,
  317.                 gheight, gx, gy);
  318.             printf("copying the pixmap into the window\n");
  319.             break;
  320.  
  321.               case XK_p:
  322.             /*
  323.             ** Make the pixmap the current rendering target.
  324.             */
  325.             glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  326.             if (!glXMakeCurrent(dpy, pm, cx)) {
  327.                 fprintf(stderr, 
  328.                    "Can't make pixmap current to context\n");
  329.                 return -1;
  330.             }
  331.             needDisplay = GL_TRUE;
  332.             printf("rendering to pixmap\n");
  333.             break;
  334.  
  335.               case XK_w:
  336.             /*
  337.             ** Make the window the current rendering target.
  338.             */
  339.             if (!glXMakeCurrent(dpy, window, cx)) {
  340.                 fprintf(stderr, 
  341.                    "Can't make window current to context\n");
  342.                 return -1;
  343.             }
  344.             needDisplay = GL_TRUE;
  345.             printf("rendering to window\n");
  346.             break;
  347.               case XK_Escape:
  348.             return 0;
  349.             }
  350.         }
  351.         break;
  352.         }
  353.     } while (XPending(dpy) != 0);
  354.  
  355.     if (needDisplay) {
  356.         needDisplay = GL_FALSE;
  357.         DoDisplay();
  358.         glFlush();
  359.     }
  360.     }
  361. }
  362.